namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Text;
    using System.Xml;
    /// <summary>
    /// this class may meet trouble,because the Confusing of subroutine,function and property, especially when they have multi parameter
    /// </summary>
    public class DNNNode
    {
        private Hashtable m_objHashAttributes;
        private DNNNodeCollection m_objNodes;
        private XmlDocument m_objXMLDoc;
        private System.Xml.XmlNode m_objXMLNode;
        private string m_strParentNS;

        public DNNNode() : this(new XmlDocument().CreateNode(XmlNodeType.Element, "n", ""))
        {
        }

        public DNNNode(string strText) : this()
        {
            this.Text = strText;
        }

        public DNNNode(System.Xml.XmlNode objXmlNode)
        {
            this.m_objXMLNode = objXmlNode;
            this.m_objXMLDoc = objXmlNode.OwnerDocument;
        }

        public DNNNode(string NodeText, string navigateUrl)
        {
            if ((NodeText == null) || (navigateUrl == null))
            {
                throw new ArgumentNullException();
            }
            this.Text = NodeText;
            navigateUrl = navigateUrl;
        }

        internal void AssociateXmlNode(System.Xml.XmlNode objXmlNode)
        {
            this.m_objXMLNode = objXmlNode;
            this.m_objXMLDoc = objXmlNode.OwnerDocument;
        }

        public DNNNode Clone()
        {
            return this.Clone(true);
        }

        public DNNNode Clone(bool blnDeep)
        {
            return new DNNNode(this.XmlNode.CloneNode(blnDeep));
        }

        public string ToJSON()
        {
            return this.ToJSON(true);
        }

        public string ToJSON(bool blnDeep)
        {
            StringBuilder builder = new StringBuilder();
            foreach (XmlAttribute attribute in this.XmlNode.Attributes)
            {
                if (builder.Length == 0)
                {
                    builder.Append("{");
                }
                else
                {
                    builder.Append(",");
                }
                string name = attribute.Name;
                if (StringType.StrCmp(name, "txt", false) == 0)
                {
                    builder.Append("text");
                }
                else if (StringType.StrCmp(name, "tar", false) == 0)
                {
                    builder.Append("target");
                }
                else if (StringType.StrCmp(name, "tTip", false) == 0)
                {
                    builder.Append("toolTip");
                }
                else
                {
                    builder.Append(attribute.Name);
                }
                builder.Append(":");
                builder.Append("\"" + ClientAPI.GetSafeJSString(attribute.Value) + "\"");
            }
            builder.Append("};");
            return builder.ToString();
        }

        public string ToXML()
        {
            return this.XmlNode.OuterXml;
        }

        public bool BreadCrumb
        {
            get
            {
                return BooleanType.FromString(this.get_CustomAttribute("bcrumb", "0"));
            }
            set
            {
                this.set_CustomAttribute("bcrumb", StringType.FromObject(Interaction.IIf(value, "1", "0")));
            }
        }
        //TODO Because the different grammer from vb and C#, we must do extra work to fix them 
        public string get_CustomAttribute(string Key)
        {
            if (this.XmlNode.Attributes.GetNamedItem(Key) != null)
            {
                return this.XmlNode.Attributes.GetNamedItem(Key).Value;
            }
            return null;
        }


        public string get_CustomAttribute(string Key, string DefaultValue)
        {
            if (Strings.Len(this.get_CustomAttribute(Key)) == 0)
            {
                return DefaultValue;
            }
            return this.get_CustomAttribute(Key);
        }




        public void set_CustomAttribute(string Key, string Value)
        {
            try
            {
                if (this.XmlNode.Attributes.GetNamedItem(Key) != null)
                {
                    if (Value == null)
                    {
                        this.XmlNode.Attributes.Remove((XmlAttribute)this.XmlNode.Attributes.GetNamedItem(Key));
                    }
                    else
                    {
                        this.XmlNode.Attributes.GetNamedItem(Key).Value = Value;
                    }
                }
                else if (Value != null)
                {
                    XmlAttribute node = this.XMLDoc.CreateAttribute(Key);
                    node.Value = Value;
                    this.XmlNode.Attributes.Append(node);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                throw exception;
            }
        }
        public void set_CustomAttribute(string Key, string DefaultValue, string Value)
        {
            if (StringType.StrCmp(Value, DefaultValue, false) == 0)
            {
                Value = "";
            }
            this.set_CustomAttribute(Key, Value);
        }

 

 

 

 

 



        public eClickAction ClickAction
        {
            get
            {
                if (Strings.Len(this.get_CustomAttribute("ca")) > 0)
                {
                    return (eClickAction) IntegerType.FromString(this.get_CustomAttribute("ca"));
                }
                return eClickAction.PostBack;
            }
            set
            {
                this.set_CustomAttribute("ca", StringType.FromInteger((int) value));
            }
        }

        public string ClientID
        {
            get
            {
                return this.ID.Replace(":", "_");
            }
        }

        public string CSSClass
        {
            get
            {
                return this.get_CustomAttribute("css");
            }
            set
            {
                this.set_CustomAttribute("css", value);
            }
        }

        public string CSSClassHover
        {
            get
            {
                return this.get_CustomAttribute("cssHover");
            }
            set
            {
                this.set_CustomAttribute("cssHover", value);
            }
        }

        public string CSSClassSelected
        {
            get
            {
                return this.get_CustomAttribute("cssSel");
            }
            set
            {
                this.set_CustomAttribute("cssSel", value);
            }
        }

        public string CSSIcon
        {
            get
            {
                return this.get_CustomAttribute("cssIcon");
            }
            set
            {
                this.set_CustomAttribute("cssIcon", value);
            }
        }

        public string this[string Key]
        {
            get
            {
                if (this.XmlNode.Attributes.GetNamedItem(Key) != null)
                {
                    return this.XmlNode.Attributes.GetNamedItem(Key).Value;
                }
                return null;
            }
            set
            {
                try
                {
                    if (this.XmlNode.Attributes.GetNamedItem(Key) != null)
                    {
                        if (value == null)
                        {
                            this.XmlNode.Attributes.Remove((XmlAttribute) this.XmlNode.Attributes.GetNamedItem(Key));
                        }
                        else
                        {
                            this.XmlNode.Attributes.GetNamedItem(Key).Value = value;
                        }
                    }
                    else
                    {
                        XmlAttribute node = this.XMLDoc.CreateAttribute(Key);
                        node.Value = value;
                        this.XmlNode.Attributes.Append(node);
                    }
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    throw exception;
                    ProjectData.ClearProjectError();
                }
            }
        }

        public string this[string Key, string DefaultValue]
        {
            get
            {
                if (Strings.Len(this.get_CustomAttribute(Key)) == 0)
                {
                    return DefaultValue;
                }
                return this.get_CustomAttribute(Key);
            }
            set
            {
                if (StringType.StrCmp(value, DefaultValue, false) == 0)
                {
                    value = "";
                }
                this.set_CustomAttribute(Key, value);
            }
        }

        public DNNNodeCollection DNNNodes
        {
            get
            {
                if (this.m_objNodes == null)
                {
                    this.m_objNodes = new DNNNodeCollection(this.XmlNode);
                }
                return this.m_objNodes;
            }
        }

        public bool Enabled
        {
            get
            {
                return BooleanType.FromString(this.get_CustomAttribute("enabled", "1"));
            }
            set
            {
                this.set_CustomAttribute("enabled", StringType.FromObject(Interaction.IIf(value, "1", "0")));
            }
        }

        public bool HasNodes
        {
            get
            {
                bool flag = BooleanType.FromString(this.get_CustomAttribute("hasNodes", "0"));
                if (!flag)
                {
                    return (this.DNNNodes.Count > 0);
                }
                return flag;
            }
            set
            {
                this.set_CustomAttribute("hasNodes", StringType.FromObject(Interaction.IIf(value, "1", "0")));
            }
        }

        public string ID
        {
            get
            {
                return this.get_CustomAttribute("id");
            }
            set
            {
                this.set_CustomAttribute("id", value);
            }
        }

        public string Image
        {
            get
            {
                return this.get_CustomAttribute("img");
            }
            set
            {
                this.set_CustomAttribute("img", value);
            }
        }

        public bool IsBreak
        {
            get
            {
                return BooleanType.FromString(this.get_CustomAttribute("break", "0"));
            }
            set
            {
                this.set_CustomAttribute("break", StringType.FromObject(Interaction.IIf(value, "1", "0")));
            }
        }

        public bool IsInHierarchy
        {
            get
            {
                return (this.XmlNode.ParentNode != null);
            }
        }

        public string JSFunction
        {
            get
            {
                return this.get_CustomAttribute("js");
            }
            set
            {
                this.set_CustomAttribute("js", value);
            }
        }

        public string Key
        {
            get
            {
                return this.get_CustomAttribute("key");
            }
            set
            {
                this.set_CustomAttribute("key", value);
            }
        }

        public int Level
        {
            get
            {
                if (this.ParentNode != null)
                {
                    System.Xml.XmlNode xmlNode = this.XmlNode;
                    int num2 = -1;
                    while ((xmlNode != null) && (xmlNode is XmlElement))
                    {
                        num2++;
                        xmlNode = xmlNode.ParentNode;
                        if ((xmlNode != null) && (StringType.StrCmp(xmlNode.Name, "root", false) == 0))
                        {
                            return num2;
                        }
                    }
                    return num2;
                }
                return -1;
            }
        }

        public string NavigateURL
        {
            get
            {
                return this.get_CustomAttribute("url");
            }
            set
            {
                this.set_CustomAttribute("url", value);
            }
        }

        public string ParentNameSpace
        {
            get
            {
                if (Strings.Len(this.m_strParentNS) == 0)
                {
                    this.m_strParentNS = "";
                    if ((this.XmlNode.ParentNode != null) && (this.XmlNode.ParentNode is XmlElement))
                    {
                        this.m_strParentNS = this.XmlNode.ParentNode.Attributes.GetNamedItem("id").Value;
                    }
                }
                return this.m_strParentNS;
            }
        }

        public DNNNode ParentNode
        {
            get
            {
                if ((this.XmlNode.ParentNode != null) && (this.XmlNode.ParentNode.NodeType != XmlNodeType.Document))
                {
                    return new DNNNode(this.XmlNode.ParentNode);
                }
                return null;
            }
        }

        private System.Xml.XmlNode RootNode
        {
            get
            {
                return this.XMLDoc.ChildNodes[0];
            }
        }

        public bool Selected
        {
            get
            {
                return BooleanType.FromString(this.get_CustomAttribute("selected", "0"));
            }
            set
            {
                this.set_CustomAttribute("selected", StringType.FromObject(Interaction.IIf(value, "1", "0")));
            }
        }

        public string Target
        {
            get
            {
                return this.get_CustomAttribute("tar");
            }
            set
            {
                this.set_CustomAttribute("tar", value);
            }
        }

        public string Text
        {
            get
            {
                return this.get_CustomAttribute("txt");
            }
            set
            {
                this.set_CustomAttribute("txt", value);
            }
        }

        public string ToolTip
        {
            get
            {
                return this.get_CustomAttribute("tTip");
            }
            set
            {
                this.set_CustomAttribute("tTip", value);
            }
        }

        internal XmlDocument XMLDoc
        {
            get
            {
                return this.m_objXMLDoc;
            }
        }

        internal System.Xml.XmlNode XmlNode
        {
            get
            {
                return this.m_objXMLNode;
            }
        }

        public XmlNodeList XmlNodes
        {
            get
            {
                return this.XmlNode.ChildNodes;
            }
        }
    }
}

